$(function () {
    var types = {
        MENU: '菜单',
        FUNCTION: "功能",
        BLOCK: '区域'
    };
    var permissiontreegrid = $("#permissionGrid",this);
    permissiontreegrid.treegrid({
        fit: true,
        idField: 'id',
        border: false,
        url: '/system/permission/list',
        treeField: 'name',
        columns: [[
            {field: 'name', title: '资源名称', width: 180},
            {field: 'permissionKey', title: '标识', width: 100},
            {
                field: 'type', title: '类型', width: 80, formatter: function (val) {
                return types[val];
            }
            },
            {field: 'path', title: '路径', width: 180},
            {field: 'resource', title: '资源', width: 180},
            {field: 'weight', title: '权重', width: 80},
            {field: 'description', title: '描述', width: 180},
            {
                field: 'enable', title: '状态', width: 80, align: 'center', formatter: function (val) {
                return val ? '可用' : '禁用';
            }
            },
            {
                field: 'edit', title: '操作', align: 'center', width: 150, formatter: function (val, row) {
                var btns = [];
                btns.push('<a class="actions fa fa-pencil-square-o edit" data-id="' + row.id + '">编辑</a>');
                btns.push('<a class="actions fa fa-trash-o delete" data-id="' + row.id + '">删除</a>');
                return btns.join("");
            }
            }
        ]],
        toolbar: [{
            text: '创建权限',
            handler: function () {
                formDialog();
            }
        }]
    });

    //获取grid面板
    var gridPanel = permissiontreegrid.treegrid("getPanel",this);
    //编辑
    gridPanel.on("click", "a.edit", function () {
        var id = this.dataset.id;
        formDialog(id);
    }).on("click", "a.delete", function () {
        var id = this.dataset.id;
        $.messager.confirm("提示","是否删除",function (r) {
            if(r){
                $.get("/system/permission/delete?id="+id).success(function () {
                    //删除成功
                    //刷新数据
                    permissiontreegrid.treegrid("reload");
                })
            }
        })
    });

    /**
     * 权限表单窗口
     */
    function formDialog(id) {
        console.log(id);
        var dialog = $("<div>").dialog({
            title: id?"编辑权限":"创建权限",
            href: 'system/permission/'+(id?'load?id='+id:'form'),
            width: 380,
            height: 450,
            /**
             * 销毁窗口
             */
            onClose: function () {
                $(this).dialog('destroy');
            },
            buttons: [
                {
                    text: '保存',
                    handler: function () {
                        var permission = $("#permissionform");
                        if (permission.form("validate")) {
                            $.post("/system/permission/"+(id?'update?':'save'),
                                permission.serialize()
                            ).success(function () {
                                //刷新列表
                                permissiontreegrid.treegrid("reload");
                                //关于窗口
                                dialog.dialog("close")
                            })
                        }
                    }
                }
            ]
        });
    }
});